package cn.yuanqiao.web.controller.activiti;


import cn.yuanqiao.activiti.domain.ActHistoryTaskDTO;
import cn.yuanqiao.activiti.domain.ActTaskDTO;
import cn.yuanqiao.activiti.domain.dto.ActWorkflowFormDataDTO;
import cn.yuanqiao.activiti.service.IActTaskService;
import cn.yuanqiao.activiti.util.SecurityUtil;
import cn.yuanqiao.common.annotation.Anonymous;
import cn.yuanqiao.common.core.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.core.page.PageDomain;
import cn.yuanqiao.common.core.page.TableDataInfo;
import cn.yuanqiao.common.core.page.TableSupport;
import com.github.pagehelper.Page;
import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/activiti/task")
public class TaskController extends BaseController {

    @Autowired
    private IActTaskService actTaskService;

    @Autowired
    private TaskService taskService;

    //获取我的代办任务
    @GetMapping(value = "/list")
    public TableDataInfo getTasks(@RequestParam(value = "instanceName",required = false) String instanceName,@RequestParam(value = "createTimeStart",required = false) String createTimeStart,@RequestParam(value = "createTimeEnd",required = false) String createTimeEnd) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        pageDomain.setOrderByColumn("createTime");
        Map<String,Object> queryVariables = new HashMap<>();
        if (instanceName!=null){
            queryVariables.put("instanceName",instanceName);
        }
        if (createTimeStart!=null){
            queryVariables.put("createTimeStart",createTimeStart);
        }
        if (createTimeEnd!=null){
            queryVariables.put("createTimeEnd",createTimeEnd);
        }
        Page<ActTaskDTO> hashMaps = actTaskService.selectProcessDefinitionList(pageDomain,queryVariables);
        return getDataTable(hashMaps);
    }

    //获取我的代办任务
    @GetMapping(value = "/getTaskById/{taskId}")
    public ActTaskDTO getTaskById(@PathVariable("taskId")String taskId) {
        ActTaskDTO taskDTO = actTaskService.getTaskById(taskId);
        return taskDTO;
    }

    //获取我的代办任务
    @GetMapping(value = "/hisTasklist")
    public TableDataInfo getHisTasklist(@RequestParam(value = "instanceName",required = false) String instanceName) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        pageDomain.setOrderByColumn("createTime");
        Page<ActHistoryTaskDTO> all = actTaskService.selectProcessDefinitionListHistoyByCode(pageDomain, "all", instanceName);
        return getDataTable(all);
    }

    //获取我的代办任务
    @GetMapping(value = "/getHisTaskById/{taskId}")
    public ActHistoryTaskDTO getHisTaskById(@PathVariable("taskId")String taskId) {
        ActHistoryTaskDTO taskDTO = actTaskService.getHisTaskById(taskId);
        return taskDTO;
    }


    //渲染表单
    @GetMapping(value = "/formDataShow/{taskID}")
    public AjaxResult formDataShow(@PathVariable("taskID") String taskID) {

        return AjaxResult.success(actTaskService.formDataShow(taskID));
    }


    //保存表单
    @PostMapping(value = "/formDataSave/{taskID}")
    public AjaxResult formDataSave(@PathVariable("taskID") String taskID,
                                   @RequestBody   List<ActWorkflowFormDataDTO> formData ) throws ParseException {
        int i = actTaskService.formDataSave(taskID, formData);
        if (i==0){
            return AjaxResult.warn("任务不存在或已办结");
        }else {
            return AjaxResult.success(i);
        }
    }

    //批量保存表单
    @PostMapping(value = "/formDataSaveAll/{taskIDs}")
    public AjaxResult formDataSaveAll(@PathVariable("taskIDs") String[] taskIDs,
                                      @RequestBody   List<ActWorkflowFormDataDTO> formData ) throws ParseException {
        int success = 0;
        for (String taskID : taskIDs) {
            if (actTaskService.formDataSave(taskID, formData)!=0){
                success++;
            }
        }
        return AjaxResult.success("提交了"+taskIDs.length+"条数据，成功审批"+success+"条");

    }

    /**
     * 通过taskId获取当前任务的下一个可选项（同意、不同意、办结）
     */
    @GetMapping(value = "/getNextTaskOption/{taskId}")
    public AjaxResult getNextTaskOption(@PathVariable("taskId") String taskId) {
        return AjaxResult.success(actTaskService.getNextTaskOption(taskId));
    }
}
